/**
 * fulihui.com Inc.
 * Copyright (c) 2015-2016 All Rights Reserved.
 */
package com.fulihui.integral.core.biz.conv;

import com.fulihui.integral.core.common.utils.ISRuleParseUtil;
import com.fulihui.integral.core.dal.dataobj.ISRuleInfo;
import com.fulihui.integral.core.dto.ISBaseRuleDTO;

import java.util.List;
import java.util.stream.Collectors;

public class ISRuleConvert {

    /**
     * DTO转换成DO
     *
     * @param source
     * @return
     */
    public static ISRuleInfo convertDTO2DO(ISBaseRuleDTO source) {
        if (source == null) {
            return null;
        }
        ISRuleInfo target = new ISRuleInfo();
        target.setId(source.getRuleId());
        target.setRuleTemplate(ISRuleParseUtil.toJSON(source.getRuleData()));
        target.setChannel(source.getChannel());
        target.setRuleSwitch(source.getRuleSwitch());
        target.setRuleType(source.getRuleType());
        target.setOperator(source.getOperator());
        target.setGmtModifed(source.getGmtModifed());
        return target;
    }

    /**
     * DO转换成DTO
     *
     * @param source
     * @return
     */
    public static ISBaseRuleDTO convertDO2DTO(ISRuleInfo source) {
        if (source == null) {
            return null;
        }
        ISBaseRuleDTO target = new ISBaseRuleDTO();
        target.setChannel(source.getChannel());
        target.setRuleType(source.getRuleType());
        target.setRuleId(source.getId());
        target.setRuleSwitch(source.getRuleSwitch());
        target.setRuleData(ISRuleParseUtil.toMap(source.getRuleTemplate()));
        target.setOperator(source.getOperator());
        target.setGmtModifed(source.getGmtModifed());
        return target;
    }

    /**
     * doList转换为dtoList
     *
     * @param list
     * @return
     */
    public static List<ISBaseRuleDTO> convert2List(List<ISRuleInfo> list) {
        return list.stream().map(ISRuleConvert::convertDO2DTO)
                .collect(Collectors.toList());
    }
}
